let box = document.querySelector(".box");
let nav = document.querySelector("nav");
let btn_nav = document.querySelector(".nav");
let flag = false;
// 导航栏
btn_nav.addEventListener("click", function () {
    flag = !flag;
    if (flag) {
        box.style.transform = `translate(200px)`;
        return;
    }
    box.style.transform = ``;
})
let index = 0;
// 用来装添加用户的数据
let newData = [];
let tbody = document.querySelector("tbody");
let tfoot = document.querySelector("tfoot");
// 渲染页面
function BindHtml() {
    tbody.innerHTML = "";
    tfoot.innerHTML = "";
    newData.forEach(function (item) {
        tbody.innerHTML += `
        <tr><td>${item.name}</td>
        <td>$${item.money.toLocaleString()}.00</td></tr>`;
    })
}
// 添加用户
let add = document.querySelector(".add");
add.addEventListener("click", function () {
    if (index >= dataInfo.length) {
        return alert("没有更多的数据了");
    }
    newData.push(dataInfo[index]);
    BindHtml();
    index++;
})
// 资金翻倍
let double = document.querySelector(".double");
double.addEventListener("click", function () {
    newData.forEach(function (item) {
        item.money *= 2;
    })
    BindHtml();
})
// 查询百万富翁
let query = document.querySelector(".query");
query.addEventListener("click", function () {
    let millionData = newData.filter(function (item) {
        return item.money >= 1000000;
    })
    tbody.innerHTML = "";
    millionData.forEach(function (item) {
        tbody.innerHTML += `
        <tr><td>${item.name}</td>
        <td>$${item.money.toLocaleString()}.00</td></tr>`
    })
})
// 财富榜
let list = document.querySelector(".list");
list.addEventListener("click", function () {
    newData = newData.sort(function (a, b) {
        return b.money - a.money;
    })
    BindHtml();
})
// 计算总额
let total = document.querySelector(".total");
total.addEventListener("click", function () {
    let totalNum = 0;
    newData.forEach(function (item) {
        totalNum += item.money;
    })
    // 渲染总额
    tfoot.innerHTML = "";
    tfoot.innerHTML = `
    <tr><td>Total sum:</td>
    <td>$${totalNum.toLocaleString()}.00</td></tr>`
})
// let a = 123456789;
// let res = (a.toFixed(2));
// console.log(res);